package com.zjb.accountManager.controllers;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.zjb.accountManager.beans.model.Record;
import com.zjb.accountManager.beans.model.User;
import com.zjb.accountManager.intercepters.CheckLogin;

public class Cp0001 extends Controller {
	
	@Before(CheckLogin.class)
	public void index () {
		this.renderJsp("cp0001.jsp");
	}
	
	public void loadData() {
		Integer userId = this.getSessionAttr("userId");
		List<Record> listRecord = Record.dao.loadData(userId);
		this.renderJson(listRecord);
	}
	
	public void save() {
		String sysName = this.getPara("sysName");
		String sysAccount = this.getPara("sysAccount");
		String sysPassword = this.getPara("sysPassword");
		String prompt = this.getPara("prompt");		
		Date updateTime = new Date();
		
		String account = this.getSessionAttr("account");
		User user = User.dao.findAccount(account);
		Integer userId = this.getSessionAttr("userId");
		String password = user.getPassword();
		
		byte[] encryptResult =AES.encrypt(sysPassword, password); // 加密
		sysPassword =AES.parseByte2HexStr(encryptResult); // 将二进制转换成16进制
		
		Boolean result = Record.dao.saveRecord(userId, sysName, sysAccount, sysPassword, prompt, updateTime);
		
		this.renderJson(result);
	}
	
	public void delete() {
		Integer id = this.getParaToInt("id");
		Boolean result = Record.dao.deleteById(id);
		this.renderJson(result);
	}
	
	public void update() {
		Integer id = this.getParaToInt("id");
		String sysName = this.getPara("sysName");
		String sysAccount = this.getPara("sysAccount");
		String sysPassword = this.getPara("sysPassword");
		String prompt = this.getPara("prompt");		
		Date updateTime = new Date();
		
		String account = this.getSessionAttr("account");
		Integer userId = this.getSessionAttr("userId");
		User user = User.dao.findAccount(account);
		String password = user.getPassword();
		
		byte[] encryptResult =AES.encrypt(sysPassword, password); // 加密
		sysPassword =AES.parseByte2HexStr(encryptResult); // 将二进制转换成16进制
		
		Boolean result = Record.dao.updateRecord(id, sysName, sysAccount, sysPassword, prompt, updateTime, userId);
		
		this.renderJson(result);
	}
	
	public void seePassword() {
		Integer id = this.getParaToInt("id");
		
		String account = this.getSessionAttr("account");
		User user = User.dao.findAccount(account);
		String password = user.getPassword();
		
		List<Record> listRecord = Record.dao.findSysPassword(id);
		String sysPassword = listRecord.get(0).getSysPassword();
		
		byte[] decryptFrom = AES.parseHexStr2Byte(sysPassword);  
		byte[] decryptResult =AES.decrypt(decryptFrom,password);
		
		String result = new String(decryptResult);
		setAttr("result", result);
		this.renderJson();
	}
	
	public void searchRecord() {
		String sysNameKey = this.getPara("sysNameKey");
		
		Integer userId = this.getSessionAttr("userId");
		
		List<Record> listRecord = Record.dao.seachRecord(userId, sysNameKey);
		
		this.renderJson(listRecord);
	}
}
